package com.turing.merchant.controller;

import com.alibaba.fastjson.JSONObject;
import com.turing.merchant.entity.Good;
import com.turing.merchant.service.CustomerGoodService;
import com.turing.merchant.service.GoodService;
import enums.ErrorCode;
import org.springframework.web.bind.annotation.*;
import result.Result;

import javax.annotation.Resource;
import java.text.ParseException;

@RestController
public class GoodController {

    @Resource
    private GoodService goodService;
    @Resource
    private CustomerGoodService customerGoodService;

    @GetMapping("good/geGoodById")
    public Result geGoodById(
            @RequestParam(value = "id") String id
    ){
        return goodService.geGoodById(id);
    }

    @GetMapping("good/myGoodList")
    public Result myGoodList(
            @RequestParam(value = "merchantId",required = false) String merchantId,
            @RequestParam(value = "gdId",required = false) String gdId,
            @RequestParam(value = "cgState",required = false) Integer cgState,
            @RequestParam(value = "gdCreateTime",required = false) String gdCreateTime,
            @RequestParam(value = "pageNum",required = false) Integer pageNum,
            @RequestParam(value = "pageSize",required = false) Integer pageSize
    ){
        return goodService.myGoodList(merchantId,gdId,cgState,gdCreateTime,pageNum,pageSize);
    }

    @GetMapping("good/goodList")
    public Result goodList(
            @RequestParam(value = "merchantName",required = false) String merchantName,
            @RequestParam(value = "gdName",required = false) String gdName,
            @RequestParam(value = "priceType",required = false) Integer priceType,
            @RequestParam(value = "gdState",required = false) Integer gdState,
            @RequestParam(value = "pageNum",required = false) Integer pageNum,
            @RequestParam(value = "pageSize",required = false) Integer pageSize
    ){
        return goodService.goodList(merchantName,gdName,priceType,gdState,pageNum,pageSize);
    }

    @GetMapping("good/checkGdName")
    public boolean checkGdName(
            @RequestParam(value = "gdName") String gdName,
            @RequestParam(value = "merchantId") String merchantId,
            @RequestParam(value = "gdId",required = false) String gdId
    ){
        return goodService.checkGdName(gdName,merchantId,gdId);
    }

    @PostMapping("good/goodSave")
    public Result goodSave(@RequestBody JSONObject jsonObject){
        Good good = JSONObject.toJavaObject(jsonObject, Good.class);
        boolean save = goodService.save(good);
        if (save){
            return Result.success();
        }else {
            return Result.fail(ErrorCode.ERROR_ADD);
        }
    }

    @GetMapping("good/cgList")
    public Result cgList(
            @RequestParam(value = "merchantName",required = false) String merchantName,
            @RequestParam(value = "gdName",required = false) String gdName,
            @RequestParam(value = "priceType",required = false) Integer priceType,
            @RequestParam(value = "cusNickname",required = false) String cusNickname,
            @RequestParam(value = "pageNum",required = false) Integer pageNum,
            @RequestParam(value = "pageSize",required = false) Integer pageSize
    ){
        return customerGoodService.cgList(merchantName,gdName,priceType,cusNickname,pageNum,pageSize);
    }

    @DeleteMapping("good/deleteByIds")
    public Result deleteByIds(@RequestParam(value = "ids") String[] ids){
        return goodService.deleteByIds(ids);
    }

    @PatchMapping("good/downShelf")
    public Result downShelf(@RequestBody JSONObject jsonObject){
        return goodService.downShelf(jsonObject);
    }

    @GetMapping("good/goodListByIds")
    public Result goodListByIds(
            @RequestParam(value = "ids") String[] ids
    ){
        return goodService.goodListByIds(ids);
    }

    @GetMapping("good/goodListForPhone")
    public Result goodListForPhone(
            @RequestParam(value = "categoryId",required = false) String categoryId,
            @RequestParam(value = "cdType",required = false) Integer cdType,
            @RequestParam(value = "longitude") String longitude,
            @RequestParam(value = "latitude") String latitude,
            @RequestParam(value = "orderType") Integer orderType,
            @RequestParam(value = "cityId",required = false) Integer cityId,
            @RequestParam(value = "orderState") String orderState,
            @RequestParam(value = "pageNum",required = false) Integer pageNum,
            @RequestParam(value = "pageSize",required = false) Integer pageSize

    ){
        return goodService.goodListForPhone(categoryId,cdType,longitude,latitude,orderType,cityId,orderState,pageNum,pageSize);
    }

    @PatchMapping("good/goodEdit")
    public Result goodEdit(@RequestBody JSONObject jsonObject){
        return goodService.goodEdit(jsonObject);
    }

    @PostMapping("good/buyGood")
    public Result buyGood(@RequestBody JSONObject jsonObject) throws ParseException {
        return goodService.buyGood(jsonObject);
    }

    @PatchMapping("good/useGood")
    public Result useGood(@RequestBody JSONObject jsonObject){
        return goodService.useGood(jsonObject);
    }
}
